package com.game77.guessTheWords2.layer;

import java.util.HashMap;

import android.graphics.Typeface;

import com.game77.guessTheWords2.R;
import com.game77.guessTheWords2.core.GameSystem;
import com.game77.guessTheWords2.core.StageManager;
import com.game77.guessTheWords2.scene.MainScene;
import com.game77.guessTheWords2.scene.ToolShopScene;
import com.game77.guessTheWords2.sprite.ClassItemBlock;
import com.game77.guessTheWords2.util.ADHelper;
import com.game77.guessTheWords2.util.AppControl;
import com.game77.guessTheWords2.util.AudioHelper;
import com.game77.guessTheWords2.util.GameUtil;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYPoint;

public class ClassChooseLayer extends Layer {

	Sprite background;
	Button btnHome;
	Sprite coin;
	Label coinCount;
	Button btnFreeCoin;

	HashMap<Integer, ClassItemBlock> classItems = new HashMap<Integer, ClassItemBlock>();

	public ClassChooseLayer() {
		// 背景
		background = Sprite.make(GameSystem.tex_class_back);
		background.setAnchor(0, 0);
		background.setPosition(WYPoint.make(0, 0));
		this.addChild(background);

		btnHome = Button.make(R.drawable.btn_home, R.drawable.btn_home_press,
				this, "OnHomeClick");
		btnHome.setPosition(25, 775);
		this.addChild(btnHome);

		initClassItemBlocks();

		coin = Sprite.make(GameSystem.tex_coin_inclass);
		coin.setPosition(345, 25);
		this.addChild(coin);

		coinCount = Label.make(StageManager.instance.getGoldCount() + "", 30,
				Typeface.BOLD);
		coinCount.setAnchorX(0);
		coinCount.setColor(WYColor3B.make(255, 255, 0));
		coinCount.setPosition(375, 25);
		this.addChild(coinCount);

		btnFreeCoin = Button.make(R.drawable.btn_freecoin,
				R.drawable.btn_freecoin_press, this, "OnFreeCoinClick");
		btnFreeCoin.setPosition(78, 25);
		if (!AppControl.isSwitcherOn()) {
			this.addChild(btnFreeCoin);
		}

		// loadClassItems();
	}

	public void updateCoinCount() {
		coinCount.setText(StageManager.instance.getGoldCount() + "");
	}

	// private void loadClassItems() {
	// layer = ScrollableLayer.make();
	// layer.setRelativeAnchorPoint(true);
	// layer.setContentSize(480, 680);
	// layer.setAnchor(0, 0);
	// layer.setPosition(0, 60);
	// layer.setVertical(true);
	// layer.setHorizontal(false);
	// layer.setLeftMargin(20);
	// layer.setRightMargin(20);
	// addChild(layer);
	//
	// // create thumb bar
	// NinePatchSprite vThumb = NinePatchSprite.make(
	// Texture2D.makePNG(R.drawable.vertical_thumb),
	// WYRect.make(DP(5), DP(7), DP(1), DP(1)));
	//
	// // set thumb bar
	// layer.setVerticalThumb(vThumb);
	// layer.setThumbFadeOutTime(1f);
	//
	// for (int i = 0; i < 8; i++) {
	// int stageIndex = GameSystem.ClassCount - i;
	// ClassItemBlock classItem = new ClassItemBlock(stageIndex, 1);
	// float y = ((int) i / 2) * 180;
	// classItem.setPosition(400, y);
	// layer.addScrollableChild(classItem);
	// classItems.put(stageIndex, classItem);
	// }
	// }
	//
	// private float DP(float v) {
	// return ResolutionIndependent.resolveDp(v);
	// }

	float startY = 149.25f;
	float perY = 165.5f;
	float x1 = 133f;
	float x2 = 347f;

	private void initClassItemBlocks() {
		for (int i = 1; i <= GameSystem.ClassCount; i++) {
			float y = 800f - (((int) ((i - 1) / 2)) * perY + startY);
			float x = x1;
			if (i % 2 == 0) {
				x = x2;
			}
			ClassItemBlock classItem = new ClassItemBlock(i);
			classItem.setAnchor(0, 0);
			classItem.setPosition(x, y);
			this.addChild(classItem);
			classItems.put(i, classItem);
		}
	}

	public void OnFreeCoinClick() {
		AudioHelper.clickButton();
		// 切换到道具商店
		GameUtil.switchSceneNormalFast(ToolShopScene.make());
		ToolShopScene.make().LastScene = 2;
	}

	public void OnHomeClick() {
		AudioHelper.clickButton();
		GameUtil.switchSceneNormalFast(MainScene.make());
		ADHelper.hideAd();
	}

	public void refreshClassInfos() {
		for (int i = 1; i <= GameSystem.CurrentClassOpenCount; i++) {
			classItems.get(i).refreshClassItem();
		}

	}
}
